package com.xmut.bs.handaccount.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;

import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.bmob.User;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;

public abstract class CommonListActivity<T> extends ToolbarBaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate {

    @BindView(R.id.recycle_view)
    public
    RecyclerView mRecycleView;
    @BindView(R.id.refresh_layout)
    public
    BGARefreshLayout mRefreshLayout;

    public RecyclerView.Adapter mAdapter;

    public List<T> mComList;
    public BmobQuery<T> mBmobQuery;
    private int pageNo = 1;
    private int pageSize;
    public User mUser;

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_common_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        beInit();
        setPageSize(20);
        mBmobQuery = new BmobQuery<>();
        mComList = new ArrayList<>();
        mUser = BmobUser.getCurrentUser(User.class);
        mRecycleView.setLayoutManager(getRvLayoutManager());
        mRecycleView.setItemAnimator(new DefaultItemAnimator());
        if (getItemDecoration() != null)
            mRecycleView.addItemDecoration(getItemDecoration());
        if (hasRefresh()) {
            initRefreshLayout(mRefreshLayout, this);
            mRefreshLayout.beginRefreshing();
        } else {
            mComList.clear();
            pageNo = 1;
            getData(pageNo, pageSize);
        }
        afterInit();
    }

    public void afterInit() {
    }

    public void beInit() {

    }

    public boolean hasRefresh() {
        return true;
    }

    public BGARefreshLayout getRefreshLayout() {
        return mRefreshLayout;
    }

    protected abstract RecyclerView.ItemDecoration getItemDecoration();

    @NonNull
    protected abstract RecyclerView.LayoutManager getRvLayoutManager();

    public void showList(List<T> list) {
        mRefreshLayout.endRefreshing();
        mRefreshLayout.endLoadingMore();
        mComList.addAll(list);
        pageNo++;
        if (mAdapter == null) {
            mAdapter = new EmptyWrapper<>(getAdapter(mComList));
            ((EmptyWrapper) mAdapter).setEmptyView(R.layout.common_empty);
            mRecycleView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    public void showError(String bean) {
        mRefreshLayout.endRefreshing();
        mRefreshLayout.endLoadingMore();
        showToast(bean);
    }

    /**
     * 设置列表适配器
     */
    protected abstract RecyclerView.Adapter getAdapter(List<T> comList);

    /**
     * 设置数据源
     */
    public abstract void getData(int pageNo, int pageSize);

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        mComList.clear();
        pageNo = 0;
        getData(pageNo, pageSize);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        getData(pageNo, pageSize);
        return false;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}
